<script setup lang="ts">
import {onMounted, ref} from 'vue';
import {QuestionControllerService, RecentUpdateQuestionVO} from '../../../../generated';
import {useRouter} from 'vue-router';
import {Timer} from "@element-plus/icons-vue";

const questions = ref<RecentUpdateQuestionVO[]>([]);
const router = useRouter();
const currentIndex = ref(0);
const containerRef = ref<HTMLElement | null>(null);

const handleQuestionClick = (id: string) => {
  router.push(`/question/detail/${id}`);
};

const startScroll = () => {
  if (!questions.value.length) return;

  setInterval(() => {
    if (containerRef.value && !containerRef.value.matches(':hover')) {
      currentIndex.value = (currentIndex.value + 1) % questions.value.length;
    }
  }, 3000);
};

onMounted(async () => {
  try {
    const res = await QuestionControllerService.listRecentUpdateQuestionsUsingGet();
    if (res.data) {
      questions.value = res.data;
      startScroll();
    }
  } catch (error) {
    console.error('获取最近更新题目失败:', error);
  }
});
</script>

<template>
  <div class="recent-update-questions">
    <div class="submission-title">
      <el-icon color="#22C55E">
        <Timer/>
      </el-icon>
      最近更新
    </div>
    <div class="question-list" ref="containerRef">
      <div
          class="questions-container"
          :style="{ transform: `translateY(-${currentIndex * 32}px)` }"
      >
        <div
            v-for="question in questions"
            :key="question.id"
            class="question-item"
            @click="handleQuestionClick(question.id)"
        >
          <el-icon color="#22C55E">
            <Timer/>
          </el-icon>
          <div class="question-title">{{ question.title }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.recent-update-questions {
  background: white;
  border-radius: 8px;
  padding: 16px;
  width: 90%;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.submission-title {
  font-size: 1.125rem;
  font-weight: 600;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #303133;
}

.question-list {
  height: 224px;
  overflow: hidden;
  position: relative;
}

.questions-container {
  transition: transform 1s ease;
}

.question-item {
  height: 32px;
  padding: 4px 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
  font-size: 14px;
}

.question-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.question-list:hover .question-item:not(:hover) {
  opacity: 0.5;
}

.question-item:hover {
  color: #409EFF;
  background-color: #ecf5ff;
}

.question-list::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 32px;
  background: linear-gradient(transparent, white);
  pointer-events: none;
}

.question-list::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 32px;
  background: linear-gradient(white, transparent);
  pointer-events: none;
  z-index: 1;
}
</style>